<cfparam name = "fname" default="myform">
<cfparam name = "ffield" default="myformfield">
<cfparam name = "dir" default="/">
<cfparam name = "start" default="1">
<cfparam name = "disp" default="40">
<cfparam name = "searchtxt" default="">
<cfparam name = "aindex" default="1">
<cfparam name = "showfolders" default="0">

<cfif isdefined('cookie.folders')>
	<cfif cookie.folders IS 1>
    	<cfset showfolders = 1>
    </cfif>
	<cfif cookie.folders IS 0>
    	<cfset showfolders = 0>
    </cfif>
</cfif>

<cfset charlist = '$,1,2,3,4,5,6,7,8,9,0'>
<cfset alphalist = "*,1,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z">
<cfset usepe = 'Yes'>
<meta HTTP-EQUIV="Pragma" CONTENT="no-cache">
<meta HTTP-EQUIV="Expires" CONTENT="-1">
<script language = "javascript" type="text/javascript">
function OpenImageEditor( url )
{
	var winwidth = ( screen.width * .8);
	var winheight = ( screen.height * .8);
	var iLeft = ( screen.width  - winwidth ) / 2 ;
	var iTop  = ( screen.height - winheight ) / 2 ;

	var sOptions = "toolbar=no,status=yes,resizable=no,dependent=yes,scrollbars=1" ;
	sOptions += ",width=" + winwidth ;
	sOptions += ",height=" + winheight ;
	sOptions += ",left=" + iLeft ;
	sOptions += ",top=" + iTop ;

	window.open( url, 'FileBrowserWindow', sOptions ) ;
}

function getImage( fileUrl, fname, ffield )
{
	window.top.opener.SetUrl( fileUrl, fname, ffield ) ;
	window.top.close() ;
	window.top.opener.focus() ;
}
</script>
<cfset fullpath = "#request.basepath#images#dir#">
<cfdirectory  action = "List" directory = "#fullpath#" name = "qAllFiles" type="file" sort="name ASC" listinfo="name">
<cfdirectory  action = "List" directory = "#fullpath#" name = "qfolders" type="dir" sort="name ASC" listinfo="name">
<cfif len(trim(searchtxt)) IS 0>
  <cfquery name="qryFileNames" dbtype="query">
    SELECT * FROM qAllFiles
    <cfif NOT aindex IS '*'>
    <cfif NOT aindex IS '1'>WHERE name LIKE ('#aindex#%') OR name LIKE ('#lcase(aindex)#%')</cfif>
	<cfif aindex IS '1'>
        WHERE name LIKE ('$%')
        <cfloop list="#charlist#" index="c">
            OR name LIKE ('#c#%') 
        </cfloop>
    </cfif>
    </cfif>
    </cfquery>
  <cfelse>
  <cfquery name="qryFileNames" dbtype="query">
    SELECT * FROM qAllFiles
    WHERE name LIKE ('%#searchtxt#%')
    </cfquery>
</cfif>
<!---create the alphaindex--->
<cfsavecontent variable="alphaoutput">
<cfloop list="#alphalist#" index="ltr" delimiters=",">
<cfquery name="qcount" dbtype="query">
    SELECT count(*) AS indextotal FROM qAllFiles
    <cfif NOT ltr IS '*'>
    <cfif NOT ltr IS '1'>WHERE name LIKE ('#ltr#%') OR name LIKE ('#lcase(ltr)#%')</cfif>
    <cfif ltr IS '1'>
        WHERE name LIKE ('$%')
        <cfloop list="#charlist#" index="c">
            OR name LIKE ('#c#%') 
        </cfloop>
    </cfif>
	</cfif>
    </cfquery>
<cfif qcount.indextotal GT 0>
<cfoutput>

<span <cfif ltr IS '*'>title="All images" alt="All images"<cfelse><cfif ltr IS '1'>title="Images starting with numbers or symbols" alt="Images starting with numbers or symbols"<cfelse>title="Images starting #ltr#" alt="Images starting #ltr#"</cfif></cfif> id="alphaindex" class="alphabox" onmouseover="this.className='alphabox_over';" onmouseout="this.className='alphabox';" onclick="window.location.href = 'index.cfm?c=imagelibrary&a=default&aindex=#ltr#&dir=#dir#';" <cfif ltr IS aindex>style="background: ##FFFFCC;"</cfif>><a href = "index.cfm?c=imagelibrary&a=default&aindex=#ltr#&dir=#dir#">#ltr#</a></span></cfoutput>
<cfelse>
<cfoutput><span id="alphaindex_disabled" class="alphabox"><a href = "##">#ltr#</a></span></cfoutput>
</cfif>
</cfloop>
</cfsavecontent>
<cfif listlen(dir, "/") GT 1>
	<cfset isdisabled = 1>
<cfelse>
	<cfset isdisabled = 0>
</cfif>
<div id="component_menus">
  <cfinvoke component="cfcs.common.buttons" method="show" width="125" btext="Upload Files" tooltip="Upload Images" jscript="collapse('searchform'); collapse('newfolder'); simpleexpandcollapse('fileupload');" img="upload.png" />
  <cfinvoke component="cfcs.common.buttons" method="show" width="125" btext="Search Images" tooltip="Search Images" jscript="collapse('fileupload'); collapse('newfolder'); simpleexpandcollapse('searchform');" img="view.png" />
  <cfinvoke component="cfcs.common.buttons" method="show" width="125" btext="New Folder" tooltip="Create a New Folder Here" jscript="collapse('searchform'); collapse('fileupload'); simpleexpandcollapse('newfolder');" img="folder_new.png" isdisabled = "#isdisabled#" />
  <div class="clear"></div>
</div>
<!---fileupload--->
<div style="display: none; border: 1px solid #003366; padding: 20px;" id="fileupload">
  <cfinclude template="upload.cfm">
</div>
<!---search form--->
<div style="display: none; border: 1px solid #003366; padding: 20px;" id="searchform"> 
<cfoutput>
    <form method="POST" action="index.cfm?c=imagelibrary&a=default&dir=#dir#" name="searchform">
      Find:&nbsp;
      <input type="text" size="20" value="#searchtxt#" name="searchtxt" />
      <input type="submit" name="submitbtn" value="Go" id="abutton" />&nbsp;&nbsp;<img src="images/abort.png" alt="Hide Search Form" title="Hide Search Form" onclick="collapse('searchform');" style="cursor: pointer;" align="absmiddle" />
    </form>
  </cfoutput> </div>
<!---new folder--->
<div style="display: none; border: 1px solid #003366; padding: 20px;" id="newfolder"> 
	<cfoutput>
    <form action="index.cfm?c=imagelibrary&a=createfolder&aindex=#aindex#&dir=#dir#&showfolders=1" method="post" name="createdir" id="createdir">
      <strong>Current Path</strong><br />
      #dir#
        <input type="text" size="10" name="newdir" id="newdir" />
        <input type="submit" id="abutton" value="Create" name="newdirbutton" />&nbsp;&nbsp;<img src="images/abort.png" alt="Abort" title="Abort" onclick="collapse('newfolder');" style="cursor: pointer;" align="absmiddle" />
   </form>
   </cfoutput>
   </div>
<table width="100%" cellpadding="0" cellspacing="0">
  <tr>
    <td colspan="4"><cfif NOT dir IS '/'>
        <!---figure out which directory is up one from this one--->
        <cfset PathLength = listlen(dir, "/")>
        <cfset UpdirName = ListDeleteAt(dir, PathLength, '/')>
        <cfif UpdirName IS ''>
          <cfset UpdirName = '/'>
        </cfif>
        <cfif NOT right(UpdirName, 1) IS '/'>
          <cfset updirName = "#updirName#/">
        </cfif>
        <img src="images/folder_up.png" align="absmiddle" width="24"> <cfoutput><a href = "index.cfm?c=imagelibrary&a=default&dir=#UpdirName#">[Parent Folder]</a></cfoutput>
      </cfif>
    </td>
  </tr>
  <cfoutput>
  <tr>
    <td id="columnheaders" colspan="3" style="cursor: pointer;" onclick="expandcollapse('folders','expandcollapseimg','#request.absolutepath#images/defaults/');">Folders [Current Path: #dir#]</td>
    <td id="columnheaders" align="right" style="cursor: pointer;" onclick="expandcollapse('folders','expandcollapseimg','#request.absolutepath#images/defaults/');"><img src="#request.absolutepath#images/defaults/collapse.gif" border="0" id="expandcollapseimg" /></td>
  </tr>
  </cfoutput>
  <tr>
    <td colspan="4">
    <cfif qfolders.recordcount GT 0>
    <div id="folders" <cfif showfolders IS 0>style="display: none;"</cfif>>
        <table width="100%" cellpadding="4" cellspacing="0">
          <cfoutput Query = "qfolders">
            <cfset ImageName = 'folder.png'>
            <tr class="menu_row" onMouseOver="this.className = 'menu_row_over2';" onMouseOut="this.className = 'menu_row';">
              <td width="90%" class="menu_cell" onclick="window.location.href = 'index.cfm?c=imagelibrary&a=default&dir=#dir##Name#/';"><img src="images/#ImageName#" align="absmiddle"> <a href = "index.cfm?c=imagelibrary&a=default&dir=#dir##Name#/">#Name#</a></td>
              <td width="10%" class="menu_cell"><div align="right"><a href = "##" onclick="javascript:ConfirmDeleteImageFolder('#UrlEncodedFormat(dir)#','#name#');"><img src="images/folder_delete.png" alt="Delete this folder" title="Delete this folder" border="0"></a> </div></td>
            </tr>
          </cfoutput>
        </table>
      </div>
        <cfoutput>
          <cfif showfolders IS 0><div id="expandcollapse_msg" style="display: block;" onclick="expandcollapse('folders','expandcollapseimg','#request.absolutepath#images/defaults/');">Click to Expand</div></cfif>
        </cfoutput>
      <cfelse>
      	<div id="messages_error">No Folders Found.</div>
	  </cfif>
    </td>
  </tr>
  <tr>
    <td colspan="5">
    <table width="100%" cellpadding="0" cellspacing="0" style="border-bottom: 4px solid black; margin-bottom: 13px; padding-bottom: 8px;">
        <tr>
          <td width="70%"><cfoutput>#alphaoutput#</cfoutput> </td>
        </tr>
      </table>
      </td>
  </tr>
  <tr>
    <td id="columnheaders" colspan="4"><cfoutput>Images in #dir#</cfoutput></td>
  </tr>
  <cfif NOT Dir IS '/'>
    <!---figure out which directory is up one from this one--->
    <cfset PathLength = listlen(dir, "/")>
    <cfset UpDirName = ListDeleteAt(dir, PathLength, '/')>
    <cfif UpDirName IS ''>
      <cfset UpDirName = '/'>
    </cfif>
  </cfif>
  <!---If it's not the main folder then display the list of folders in this directory--->
  <cfoutput Query = "qfolders">
    <cfset ImageName = 'folder.gif'>
  </cfoutput>
  <cfset dcount = 0>
  <cfset colcount = 0>
  <tr>
  
  <!---Show the images--->
  <cfloop query = "qryFileNames">
    <cfoutput>
    
    <!---Figure out what action to use on this file from it's file type--->
    <cfset theaction='browse'>
    <cfset theicon='document_filetype.gif'>
    <cfset thetype='none'>
    <cfif Right(name, 4) IS '.gif' OR right(name, 4) IS '.jpg' OR right(name, 4) IS '.png' or right(name,4) IS 'jpeg'>
      <cfset theaction='viewimage'>
      <cfset theicon='image_filetype.gif'>
      <cfset thetype='Image'>
    </cfif>
    <!---Display the list of files--->
    <cfif thetype IS 'Image'>
      <cfset colcount = colcount + 1>
      <cfset dCount = dCount + 1>
      <td height="150" width="25%" valign="middle" align="center" class="image_out" onMouseOver="this.className = 'image_over';" onMouseOut="this.className = 'image_out';">
      <div id="component_menus">
      <cfinvoke component="cfcs.common.buttons" method="show" width="110" btext="Edit Image" tooltip="Edit this image" url="index.cfm?c=imageeditor&dir=#dir#&imagefile=#name#&aindex=#aindex#&imagename=#fullpath#" img="edit.png" />
<cfinvoke component="cfcs.common.buttons" method="show" width="110" btext="Delete Image" tooltip="Delete this image" url="index.cfm?c=imagelibrary&a=delete&searchtxt=#searchtxt#&aindex=#aindex#&imgsrc=#dir##name#&imgfile=#name#&dir=#dir#" img="delete.png" />
	  <div class="clear"></div>
      </div>
      <table width="100%" cellpadding="0" cellspacing="0" align="center">
          <tr onclick="window.location.href = 'index.cfm?c=imageeditor&dir=#dir#&imagefile=#name#&aindex=#aindex#&imagename=#fullpath#';">
            <td height="115" align="center">
             <cfset imgsrc = "#request.HomeURL#images#dir##name#">
              <div align="center" style="height: 150px; overflow: hidden; width: 175px; cursor: pointer;">
                <center>
                  <img src = "#imgsrc#" style="max-width: 175px;" border="0" alt="#name# - click to edit" title="#name# - click to edit" onclick="window.location.href = 'index.cfm?c=imageeditor&dir=#dir#&imagefile=#name#&aindex=#aindex#'">
                </center>
                <br />
              </div>
              <center>
                <span style="cursor:pointer;">#name#</span>
              </center>
              </a>
			</td>
          </tr>
        </table></td>
      <cfif colcount GT 3>
        </tr>
        
        <tr>
        
        <cfset colcount = 0>
      </cfif>
    </cfif>
    </cfoutput>
    
  </cfloop>
</table>
